package cn.lili.xiaoran.controller;

import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.mybatis.util.PageUtil;
import cn.lili.xiaoran.entity.LiQuestions;
import cn.lili.xiaoran.entity.LiQuestionsDetail;
import cn.lili.xiaoran.entity.dtos.LiQuestionsDetailDTO;
import cn.lili.xiaoran.service.LiQuestionsDetailService;
import cn.lili.xiaoran.service.LiQuestionsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 提问表(LiQuestionsDetail)表控制层
 *
 * @author makejava
 * @since 2022-08-14 13:52:39
 */
@RestController
@Api(tags = "liQuestionsDetail接口")
@RequestMapping("/manager/lili/liQuestionsDetail")
@Transactional(rollbackFor = Exception.class)
public class LiQuestionsDetailMangerController {
    /**
     * 服务对象
     */
    @Resource
    private LiQuestionsDetailService liQuestionsDetailService;

    @Resource
    private LiQuestionsService liQuestionsService;

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看${tableInfo.description}详情")
    public ResultMessage<LiQuestionsDetail> get(@PathVariable Integer id) {

        LiQuestionsDetail liQuestionsDetail = liQuestionsDetailService.getById(id);
        return new ResultUtil<LiQuestionsDetail>().setData(liQuestionsDetail);
    }

    @GetMapping
    @ApiOperation(value = "分页获取${tableInfo.description}")
    public ResultMessage<IPage<LiQuestionsDetail>> getByPage(LiQuestionsDetailDTO page) {
        IPage<LiQuestionsDetail> data = this.liQuestionsDetailService.page(PageUtil.initPage(page), page.queryWrapper());
        return new ResultUtil<IPage<LiQuestionsDetail>>().setData(data);
    }

    @PostMapping
    @ApiOperation(value = "新增${tableInfo.description}")
    public ResultMessage<LiQuestionsDetail> save( LiQuestionsDetail liQuestionsDetail) {
        liQuestionsDetail.setIsfaqirenweidu(true);
        if (this.liQuestionsDetailService.save(liQuestionsDetail)) {
            return new ResultUtil<LiQuestionsDetail>().setData(liQuestionsDetail);
        }
        return new ResultUtil<LiQuestionsDetail>().setErrorMsg("未知异常，请稍后重试");
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新${entity.description}")
    public ResultMessage<LiQuestionsDetail> update(@PathVariable String id, LiQuestionsDetail liQuestionsDetail) {
        if(liQuestionsDetail.getChakanshu()!=null){
            LiQuestions qq=new LiQuestions();
            qq.setId(liQuestionsDetail.getQuestionsId());
            qq.setChakannum(liQuestionsDetail.getChakanshu());
            liQuestionsService.update(qq);
        }
        if (this.liQuestionsDetailService.updateById(liQuestionsDetail)) {
            return new ResultUtil<LiQuestionsDetail>().setData(liQuestionsDetail);
        }
        return new ResultUtil<LiQuestionsDetail>().setErrorMsg("未知异常，请稍后重试");
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除提问表")
    public ResultMessage<Object> delAllByIds(@PathVariable List ids) {

        liQuestionsDetailService.removeByIds(ids);
        return ResultUtil.success("成功删除");
    }


}

